//
//  CIconView.m
//  IconMaster
//
//  Created by 为为 on 13-6-6.
//  Copyright (c) 2013年 为为. All rights reserved.
//

#import "CIconView.h"
#import "CImageUtil.h"

@implementation CIconView

+ (CIconView *)iconViewFromNib {
    CIconView* iconView = (CIconView*)GetObjectFromXib([CIconView class], @"CIconView");
    return iconView;
}

+ (CIconView *)iconViewFromNibWithCornorRadius {
    CIconView* iconView = (CIconView*)GetObjectFromXib([CIconView class], @"CIconView");
    iconView.layer.cornerRadius = ICON_RADIUS;
    iconView.layer.masksToBounds = true;
    return iconView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder:aCoder];
    [aCoder encodeObject:backgroundImageView forKey:STRINGIFY(backgroundImageView)];
    if (backgroundImageView.image) {
        [aCoder encodeObject:UIImagePNGRepresentation(backgroundImageView.image) forKey:STRINGIFY(backgroundImageView.image)];
    }
    
    [aCoder encodeObject:frameImageView forKey:STRINGIFY(frameImageView)];
    if (frameImageView.image) {
        [aCoder encodeObject:UIImagePNGRepresentation(frameImageView.image) forKey:STRINGIFY(frameImageView.image)];
    }
    
    [aCoder encodeObject:gradientView forKey:STRINGIFY(gradientView)];
    [aCoder encodeObject:spriteContainer forKey:STRINGIFY(spriteContainer)];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
	self = [super initWithCoder:aDecoder];
	if (self) {
        NSData* imageData = nil;
        
        backgroundImageView = [aDecoder decodeObjectForKey:STRINGIFY(backgroundImageView)];
        backgroundImageView.backgroundColor = [UIColor clearColor];
        imageData =[aDecoder decodeObjectForKey:STRINGIFY(backgroundImageView.image)];
        if (imageData) {
            backgroundImageView.image = [UIImage imageWithData:imageData];
        }
        
        frameImageView = [aDecoder decodeObjectForKey:STRINGIFY(frameImageView)];
        imageData =[aDecoder decodeObjectForKey:STRINGIFY(frameImageView.image)];
        if (imageData) {
            frameImageView.image = [UIImage imageWithData:imageData];
        }
        
        gradientView = [aDecoder decodeObjectForKey:STRINGIFY(gradientView)];
        spriteContainer = [aDecoder decodeObjectForKey:STRINGIFY(spriteContainer)];
        
        self.backgroundColor = [UIColor clearColor];
	}
	return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (UIImage *)getBackgroundImage
{
	return backgroundImageView.image;
}

- (void)setBackgroundImageWithName:(NSString *)imageName
{
	UIImage* image = [UIImage imageNamed:imageName];
	backgroundImageView.image = image;
}

- (void)setBackgroundImage:(UIImage *)image
{
	backgroundImageView.image = image;
}

- (void)setBackgroundImageViewColor:(UIColor*)color {
    backgroundImageView.backgroundColor = color;
}

- (UIImage*)getFrameImage {
    return frameImageView.image;
}

- (void)setFrameImageWithName:(NSString *)imageName
{
	UIImage* image = [UIImage imageNamed:imageName];
	frameImageView.image = image;
}

- (void)setFrameImage:(UIImage *)image
{
	frameImageView.image = image;
}

- (void)addSpriteWithImage:(UIImage *)image
{
	SpriteView* spriteView;
	
	spriteView = [[SpriteView alloc] init];
	[spriteView setImage:image];
	[self addSprite:spriteView];
}

- (void)addSpriteWithName:(NSString *)imageName
{
	UIImage* image = [CImageUtil imageFromFile:imageName];
	[self addSpriteWithImage:image];
}

- (void)addSprite:(SpriteView *)sprite
{
	sprite.center = CGPointMake(spriteContainer.bounds.size.width/2, spriteContainer.bounds.size.height/2);
	[spriteContainer addSprite:sprite];
}

- (SpriteContainerView *)spriteContainer
{
	return spriteContainer;
}

- (CAGradientLayer*)getGradientLayer
{
	NSArray* layers = [gradientView.layer sublayers];
	CAGradientLayer* gradientlayer = [layers lastObject];
	if (!gradientlayer) {
		gradientlayer = [[CAGradientLayer alloc] init];
		gradientlayer.frame = gradientView.frame;
		[gradientView.layer addSublayer:gradientlayer];
		gradientlayer.colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor clearColor], nil];
		gradientlayer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:1.0], nil];
		gradientlayer.startPoint = CGPointMake(0.5, 0);
		gradientlayer.endPoint = CGPointMake(0.5, 1);
	}
	return gradientlayer;
}

- (void)setGradientColor:(UIColor *)color
{
	CAGradientLayer* layer = [self getGradientLayer];
	if (layer) {
//		layer.colors = [NSArray arrayWithObjects:(id)color.CGColor, (id)[UIColor clearColor].CGColor, nil];
		UIColor* glossColor1 = [UIColor colorWithRed:1.0 green:1.0
												  blue:1.0 alpha:0.65];
		UIColor* glossColor2 = [UIColor colorWithRed:1.0 green:1.0
												  blue:1.0 alpha:0.1];

		layer.colors = [NSArray arrayWithObjects:(id)glossColor1.CGColor, (id)glossColor2.CGColor, nil];
	}
}

- (void)setGradientLocation:(CGFloat)location
{
	CAGradientLayer* layer = [self getGradientLayer];
	if (layer) {
//		layer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:location], [NSNumber numberWithFloat:1.0], nil];
		layer.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:location], nil];
	}
}

- (UIImage *)iconImage {
    UIImage* image = nil; 
	image = [CImageUtil imageFromView:self];
    return image;
}

@end
